﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace LiteDBDemo
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : Form
    {
        LiteDBHelper<User> _liteDBHelper = new LiteDBHelper<User>(DataTableKey.User);
        public Form1()
        {
            InitializeComponent();
            _liteDBHelper.liteDB.EnsureIndex(t => t.id, true);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();//计算耗时
            sw.Start();
            List<User> list = new List<User>();
            for (int i = 0; i < 1000; i++)
            {
                User user = new User();
                user.name = $"7xtbScnAok7rbX86xaDgv1EQDkR9nAf7GxCJFN0lF8TnfXFhhs9z1kXe/cdk8G66VElD9g0bOBywVT1r0YRUyBQlm/5dk2FDNF+DBnUU+GZB67rKz+8rGvLsU4/uk4Cim/lPThs2CEuVu0XZJVARcMz+9ntw+xGuCNbjABXEpvu0Wp+Gx6RFdZgh2EQhFLksZoyoadeE6kR413zmo16csLZUNHOC6Ho2uuvGRt9LVPdJF0pXhsYSG6xg4DEYIeSAjE8fwd1v8V0izFWrUyWTLXdGXK5hxt/6vDXun/3Qpvi2mymRjvu7NY7b3dDvXuUFTDsLZEYb5WpYaL/LfwUGfRjkmIEy5tQXCqY7YjJvBbZN+Sl4tDq6yc/sbvuaLYjhsozTMqclWH1h+9YJQ6z7hSU/nx5+YLYeC5mdvL9INHRieHCqGnu6ru4yCfwNO6kA6Q+F3Kya6GKqKAkqndg8ZjQ4gKF213jwRNUz3yOEV6CeS9r1SUoneBp/OII5PSaykhI1h95s77gSUf50K6YN6j4XR8/Y2av1UNoc06OFAybZbspZMNGNK/FmLAozPckR/owxfhC5gsNRJx6AFp3ZrwcIVBGkdILYhHgQZw4jAowp1+yUwt28QHm2xFVbrhcZBN0g5wX1Iae9eKowPh4JOavSFiji8x3V9VE4wWL3vlw/ljDoHi4fY766nnS/FomkbPLYD2f2bBxZbRVDDytG/usdxX8OqL/5Yo1KXIsPNqlTkAqE0TTIEco+C9qvIJ77U+IiWspZgFfk5sLV5YaL8hXNjb44yF21/i7/b/0AaeH9GDpd4a1COD4BzJOINrgyKn/xvcJR91m3WKn+EHMyM1plS0N0VtCszp50QBzTjrPbIYdyg9sqNZ30LZ1GVuM/lrp1rQ+AVCSQXsUPVrqmWQ==";
                list.Add(user);
            }
           int result= _liteDBHelper.InsertAll(list);
            sw.Stop();
            string strJson = $"查询到 {result} 条数据 耗时:{sw.Elapsed.TotalSeconds}秒";
            textBox1.Text = strJson;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            Stopwatch sw = new Stopwatch();//计算耗时
            sw.Start();
            // var  list = _liteDBHelper.liteDB.Query().Where(t=>t.id>10000).OrderBy(s=>s.id).ToList();
            //var list = _liteDBHelper.liteDB.FindOne(t => t.id > 100);
            //var list = _liteDBHelper.QueryAll<User>();
            //排序查询
            //var list = _liteDBHelper.QueryDataOrderByDesc<int>(t => t.id > 10000, s => s.id, 10);
           // n => n.Name.StartsWith("Jim1")
            //分页查询 
            var list = _liteDBHelper.QueryDataByPage<int>(t=> t.id<10000, s => s.id, true, 10, 2).ToList();
            // 将 List 转换为 JSON 格式的字符串
            //string json = JsonConvert.SerializeObject(list);
            sw.Stop();
            string strJson = $"查询到 {list.Count()} 条数据 耗时:{sw.Elapsed.TotalSeconds}秒";
            textBox1.Text = strJson;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = 0;
            Stopwatch sw = new Stopwatch();//计算耗时
            sw.Start();
            var user= _liteDBHelper.QueryDataOrderByDesc<int>(t => t.id > 10000, s => s.id, 10);
            if (user != null) {
                count = user.Count();
            foreach (var item in user)
            {
                    _liteDBHelper.Delete(t => t.id == item.id);
            }
            }
            else
            {
                 count = _liteDBHelper.DeleteAll();
            }
            _liteDBHelper.Rebuild(); //重建数据库文件
           
            sw.Stop();
            textBox1.Text =$"删除 {count} 条数据 耗时:{sw.Elapsed.TotalSeconds}秒 ";
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();//计算耗时
            sw.Start();
            // var data = _liteDBHelper.QueryAll().ToList();
            //var list = _liteDBHelper.QueryData(t => t.id < 400).ToList();
           int result=  _liteDBHelper.UpdateMany(t=>new User() { timestamp=DateTime.Now.ToString()},w=>w.id>10000);
             //list = _liteDBHelper.QueryData(t => t.id < 400).ToList();
            sw.Stop();
            string strJson = $"按条件修改 {result} 条数据 耗时:{sw.Elapsed.TotalSeconds}秒";
            textBox1.Text = strJson;
        }
    }
    /// <summary>
    /// 用户实体
    /// </summary>
    public class User
    {
        public int id { get; set; }
        public string name { get; set; }
        public string timestamp { get; set; }
    }
    /// <summary>
    /// 表对象
    /// </summary>
    public class DataTableKey
    {
        /// <summary>
        /// 用户表
        /// </summary>
        public static string User => "User";
    }
    }
